<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2020-09-24 19:45:23
 * @LastEditors: Sam
 * @LastEditTime: 2020-10-20 13:42:55
 */



namespace app\admin\model\article;

use app\admin\model\store\StoreProduct;
use app\admin\model\system\SystemAdmin;
use app\admin\model\article\ArticleContent;
use traits\ModelTrait;
use basic\BaseModel;

/**
 * 图文管理 Model
 * Class WechatNews
 * @package app\admin\model\wechat
 */
class Article extends BaseModel
{

    use ModelTrait;

    protected $pk = 'id';

    protected $name = 'article';

    protected $autoWriteTimestamp = true;


    public function cate(){
        return $this->belongsTo('article_category','cid')->bind(['catename'=>'title']);
    }
    
    /**
     * 获取列表
     * @param array $where 查询条件
     * @return void
     * @author Sam
     * @date 2020-09-28
     */
    public static function getList($where = []){
        $data  = self::setWhere($where)->page((int)$where['page'],(int)$where['limit'])->order('id desc')->select();
        $data  = count($data) ? $data->toArray() : [];
        $count = self::setWhere($where)->count();
        return compact('data','count');
    }

    /**
     * 获取查询条件
     * @param $where
     * @param string $alert
     * @param null $model
     * @return SystemUserLevel|null
     */
    public static function setWhere($where,$alert='',$model=null)
    {
        $model = $model===null ? new self() : $model;
        $model = $model::with('cate');
        if ($where['title'] != '') $model = $model->where('title', 'LIKE', "%$where[title]%");
        if ($where['cid'] != '')   $model = $model->where('cid', 'in', $where['cid']);
        return $model;
    }


    /**
     * 删除
     * @param $id
     * @return bool
     */
    public static function del($id)
    {
        return self::destroy($id);
    }


}